package thread;

public class SyncDemo {
    public static void main(String[] args) {
        Table t = new Table();
        Thread t1 = new Thread("甲"){
            public void run(){
                while(true){
                    int beans =t.getBeans();
                    Thread.yield();
                    System.out.println(getName()+":"+beans);
                }
            }
        };

        Thread t2 = new Thread("乙"){
            public void run() {
                while(true){
                    int beans = t.getBeans();
                    Thread.yield();
                    System.out.println(getName()+":"+beans);
                }
            }
        };
        t1.start();
        t2.start();

    }



}
class Table{
    private int beans = 20;

    public synchronized int getBeans(){
        if (beans==0){
            throw new RuntimeException("豆子不够了");
        }
        Thread.yield();
        return beans--;
    }
}